package com.greenant.store.bean;

import java.text.DecimalFormat;

import android.text.TextUtils;

import com.avos.avoscloud.AVACL;
import com.avos.avoscloud.AVClassName;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVRole;
import com.greenant.store.utils.iLog;

@AVClassName(AntAVObject.CLASS_AVGOODS)
public class AntGoods extends AntAVObject {
	private static final String TAG = "Goods";

	public static final String ATTR_SRCID = "srcId";
	public static final String ATTR_STOREID = "userId";
	public static final String ATTR_DESCRIPTION = "description";
	public static final String ATTR_UNIT = "unit";
	// 标签显示标志：0-无标签，1-促销 2-热卖 3-售完
	public static final String ATTR_SHOW_TAG = "showTag";
	public static final String ATTR_DEDUCTPERCENT = "deductPercent";// 提成百分比

	// 分类
	public static final String ATTR_KIND = "kind";
	public static final String ATTR_STYLE = "style";
	public static final String ATTR_IMAGE_URL = "image";
	public static final String ATTR_DISCOUNT = "discount";// 折扣
	public static final String ATTR_INPRICE = "inPrice"; //进价
	public static final String ATTR_PRICE = "price";
	public static final String ATTR_ORIGINALPRICE = "originalPrice";
	// 是否上架，默认为上架
	public static final String ATTR_IS_ON_SHELF = "isOnShelf";
	// 销量
	public static final String ATTR_TOTAL_SALES = "totalSales";
	// 库存
	public static final String ATTR_STOCK = "stock";

	public static final String ATTR_UPTOWNS = "uptowns";// 服务区

	// 每次更改增加的量
	public static final String ATTR_WEIGHSTEP = "weighStep";
	// 每次更改增加的量的单位
	public static final String ATTR_WEIGHUNIT = "weighUnit";
	// 是否称重计价
	public static final String ATTR_WEIGHABLE = "weighable";

	private AVRole customRole;
	public static final String ATTR_USRID = "userId";

	public static final String ATTR_SALE_STATE = "saleState";

	public static final String ATTR_FILE = "image";
	public static final String ATTR_IS_FROM_LIB = "isFromLib";
	public static final String ATTR_SPECS = "specs";
	public static final String ATTR_GOODCODE = "serialNo";
	public static final String ATTR_ISRECOMMEND = "isRecommend";

	public AntGoods() {
		super(AntAVObject.CLASS_AVGOODS);
		setAccessCtrl();
	}

	public AntGoods(String str) {
		// TODO Auto-generated constructor stub
		super(str);
		setAccessCtrl();
	}

	public void setIsFromLib(boolean isFromLib) {
		this.put(ATTR_IS_FROM_LIB, isFromLib);
	}

	public boolean isFromLib() {
		return this.getBoolean(ATTR_IS_FROM_LIB);
	}
	
	public void setIsRecommend(boolean isRecommend) {
		this.put(ATTR_ISRECOMMEND, isRecommend);
	}
	
	public boolean isRecommend() {
		return this.getBoolean(ATTR_ISRECOMMEND);
	}
	
	public void setPrice(float price) {
		this.put(ATTR_PRICE, price);
	}

	public void setDiscount(float discount) {
		this.put(ATTR_DISCOUNT, discount);
	}

	public void setStock(int sk) {
		this.put(ATTR_STOCK, sk);
	}

	public int getSaleState() {
		return Integer.parseInt(this.getString(ATTR_SALE_STATE));
	}

	public void setSalesVolume(int state) {
		this.put(ATTR_SALE_STATE, state);
	}

	public void setShelfState(Boolean state) {
		this.put(ATTR_IS_ON_SHELF, state);
	}

	public String getUserId() {
		return this.getString(ATTR_USRID);
	}

	public void setUserId(String userId) {
		this.put(ATTR_USRID, userId);
	}
	
	public String getSpecs() {
		return this.getString(ATTR_SPECS);
	}

	public void setSpecs(String specs) {
		this.put(ATTR_SPECS, specs);
	}

	public int getDeductPercent() {
		return this.getInt(ATTR_DEDUCTPERCENT);
	}

	public void setDeductPercent(int descrip) {
		this.put(ATTR_DEDUCTPERCENT, descrip);
	}

	public double getDiscount() {
		return this.getDouble(ATTR_DISCOUNT);
	}

	public void setDiscount(double descrip) {
		this.put(ATTR_DISCOUNT, descrip);
	}

	public String getImageUrl() {
		String url = "";
		try {
			url = this.getAVFile(ATTR_IMAGE_URL).getUrl();
		} catch (Exception e) {
			iLog.e(TAG, "getImageUrl is null");
		}
		if (TextUtils.isEmpty(url)) {
			url = this.getString(ATTR_IMAGE_URL);
		}
		return url;
	}
	
	public AVFile getImageFile() {
		return this.getAVFile(ATTR_IMAGE_URL);
	}

	public void setImageFile(AVFile avFile) {
		this.put(ATTR_IMAGE_URL, avFile);
	}

	public String getDescriptionAndSpecs()
    {
    	if(!TextUtils.isEmpty(getSpecs())) {
			return this.getString(ATTR_DESCRIPTION) + "(" + getSpecs() + ")";
		}
        return this.getString(ATTR_DESCRIPTION);
    }
	
	public String getDescription() {
		return this.getString(ATTR_DESCRIPTION);
	}

	public void setDescription(String descrip) {
		this.put(ATTR_DESCRIPTION, descrip);
	}

	public void setUnit(String descrip) {
		this.put(ATTR_UNIT, descrip);
	}

	public String getUnit() {
		return this.getString(ATTR_UNIT);
	}

	public void setOriginalPrice(String unit) {
		this.put(ATTR_ORIGINALPRICE, unit);
	}

	public double getOriginalPrice() {
		return this.getDouble(ATTR_ORIGINALPRICE);
	}

	public void setPrice(String unit) {
		this.put(ATTR_PRICE, unit);
	}

	public double getPrice() {
		return this.getDouble(ATTR_PRICE);
	}
	
	public double getInPrice() {
		return this.getDouble(ATTR_INPRICE);
	}

	public double getRealPrice() {
		DecimalFormat df = new DecimalFormat("#########.##");
		if (this.getDiscount() <= 0) {
			return getPrice();
		} else {
			return Double.parseDouble(df
					.format((getPrice() * getDiscount() / 10)));
		}
	}

	public void setWeighStep(double unit) {
		this.put(ATTR_WEIGHSTEP, unit);
	}

	public double getWeighStep() {
		return this.getDouble(ATTR_WEIGHSTEP);
	}

	public void setWeighUnit(String weighUnit) {
		this.put(ATTR_WEIGHUNIT, weighUnit);
	}

	public String getWeighUnit() {
		if (isWeighAble()) {
			return this.getString(ATTR_WEIGHUNIT);
		} else {
			return getUnit();
		}

	}

	public void setWeighAble(boolean weighAble) {
		this.put(ATTR_WEIGHABLE, weighAble);
	}

	public boolean isWeighAble() {
		return this.getBoolean(ATTR_WEIGHABLE);
	}

	public void setPrice(double price) {
		this.put(ATTR_PRICE, price);
	}
	
	public void setInPrice(double inPrice) {
		this.put(ATTR_INPRICE, inPrice);
	}

	public double getStock() {
		return this.getDouble(ATTR_STOCK);
	}

	public void setStock(double sk) {
		this.put(ATTR_STOCK, sk);
	}

	public String getStyle() {
		return this.getString(ATTR_STYLE);
	}

	public void setStyle(String style) {
		this.put(ATTR_STYLE, style);
	}
	
	public String getKind() {
		return this.getString(ATTR_KIND);
	}

	public void setKind(String kind) {
		this.put(ATTR_KIND, kind);
	}

	public int getTotalSales() {
		return this.getInt(ATTR_TOTAL_SALES);
	}

	public void setTotalSales(int sales) {
		this.put(ATTR_TOTAL_SALES, sales);
	}

	public boolean getIsOnShelf() {
		return this.getBoolean(ATTR_IS_ON_SHELF);
	}

	public void setShelfState(boolean state) {
		this.put(ATTR_IS_ON_SHELF, state);
	}

	public AntGoods getInstance() {
		return this;
	}

	public String getStoreId() {
		return this.getString(ATTR_STOREID);
	}

	public void setStoreId(String userId) {
		this.put(ATTR_STOREID, userId);
	}
	
	public String getSrcId() {
		return this.getString(ATTR_SRCID);
	}

	public void setSrcId(String srcId) {
		this.put(ATTR_SRCID, srcId);
	}

	public int getShowTag() {
		return this.getInt(ATTR_SHOW_TAG);
	}
	
	public void setShowTag(int showTag) {
		this.put(ATTR_SHOW_TAG, showTag);
	}

	public void setShelfState(int tag) {
		this.put(ATTR_SHOW_TAG, tag);
	}

	public String getGoodsCode() {
		return this.getString(ATTR_GOODCODE);
	}

	public void setGoodsCode(String userId) {
		this.put(ATTR_GOODCODE, userId);
	}
	private int setAccessCtrl() {

		acl = new AVACL();
		acl.setPublicReadAccess(true);// �˴����õ��������˵Ŀɶ�Ȩ��
		acl.setPublicWriteAccess(true);

		return 0;
	}

}
